package com.huazhi.wzsnw.service.impl;

import com.huazhi.wzsnw.mapper.*;
import com.huazhi.wzsnw.model.*;
import com.huazhi.wzsnw.service.PeasantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

@Service
public class PeasantServiceImpl implements PeasantService{
    @Autowired
    private PeasantMapper peasantMapper;
    @Autowired
    private ProjectTableMapper projectTableMapper;
    @Autowired
    private VillageMapper villageMapper;
    @Autowired
    private WatercheckMapper watercheckMapper;
    @Autowired
    private LinedMapper linedMapper;
    @Autowired
    private ShuiZhiMapper shuiZhiMapper;
    @Autowired
    private XzMapper xzMapper;
    @Autowired
    private  ProAndXzMapper proAndXzMapper;


    @Override
    public List<Peasant> getPeasaneBySearch(String key, Integer page, Integer pageSize) {
        return null;
    }


    @Override
    public String getByBaseName(String name,String table) {
        return peasantMapper.getBaseByName(name,table);
    }

    @Override
    public List<Village> getVillageBySearch(String key, Integer page, Integer pageSize) {
        return null;
    }

    @Override
    public List<ProjectTable> getProjectTableBySearch(String key, Integer page, Integer pageSize) {
        return null;
    }

    @Override
    public Integer saveBase(Object object,String table){
        try {
            if("village".equals(table)){
                villageMapper.insertSelective((Village)object);
            }else if("peasant".equals(table)){
                peasantMapper.insertSelective((Peasant)object);
            }else if("projectTable".equals(table)){
                projectTableMapper.insertSelective((ProjectTable)object);
            }else if("lined".equals(table)){
                linedMapper.insertSelective((Lined) object);
            }else if("watercheck".equals(table)){
                watercheckMapper.insertSelective((Watercheck) object);
            }else if("shuizhi".equals(table)){
                shuiZhiMapper.insertSelective((ShuiZhi) object);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }

    }


    @Override
    public Integer updateBase(Object object,String table){
        try {
            if("village".equals(table)){
                villageMapper.updateByPrimaryKeySelective((Village)object);
            }else if("peasant".equals(table)){
                peasantMapper.updateByPrimaryKeySelective((Peasant)object);
            }else if("projectTable".equals(table)){
                projectTableMapper.updateByPrimaryKeySelective((ProjectTable)object);
            }else if("watercheck".equals(table)){
                watercheckMapper.updateByPrimaryKeySelective((Watercheck) object);
            }else if("lined".equals(table)){
                linedMapper.updateByPrimaryKeySelective((Lined) object);
            }else if("shuizhi".equals(table)){
                shuiZhiMapper.updateByPrimaryKeySelective((ShuiZhi) object);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }

    }

    @Override
    public int deleteBase(String tid, String table) {
        try {
            if("village".equals(table)){
                return  villageMapper.deleteByPrimaryKey(tid);
            }else if("peasant".equals(table)){
                return peasantMapper.deleteByPrimaryKey(tid);
            }else if("projectTable".equals(table)){
                return projectTableMapper.deleteByPrimaryKey(tid);
            }else if("lined".equals(table)){
                return linedMapper.deleteByPrimaryKey(tid);
            }else if("watercheck".equals(table)){
                return watercheckMapper.deleteByPrimaryKey(tid);
            }else if("shuizhi".equals(table)){
                return shuiZhiMapper.deleteByPrimaryKey(tid);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return -1;
        }
    }


    @Override
    public Object findBaseByTid(String tid, String table) {

        try {
            if("village".equals(table)){
                return  villageMapper.selectByPrimaryKey(tid);
            }else if("peasant".equals(table)){
                return peasantMapper.selectByPrimaryKey(tid);
            }else if("projectTable".equals(table)){
                return projectTableMapper.selectByPrimaryKey(tid);
            }else if("watercheck".equals(table)){
                return watercheckMapper.selectByPrimaryKey(tid);
            }else if("lined".equals(table)){
                return linedMapper.selectByPrimaryKey(tid);
            }else if("shuizhi".equals(table)){
                return shuiZhiMapper.selectByPrimaryKey(tid);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

        return  null;


    }

    @Override
    public List<String> getListBaseTidBySearch(String key, String table) {
        try {
            if("village".equals(table)){
                return  villageMapper.selectListTidBySearch(key);
            }else if("peasant".equals(table)){
                return peasantMapper.selectListTidBySearch(key);
            }else if("projectTable".equals(table)){
                String str ="7f8fd81bf61c";
                if(key.startsWith(str)){
                   key = key.replace(str,"");
                   List<String> proIds = getProIDs(key);
                   return projectTableMapper.seleByProId(proIds);
                }
                return projectTableMapper.selectListTidBySearch(key,null);
            }else if("watercheck".equals(table)){
                return watercheckMapper.selectListTidBySearch(key);
            }else if("lined".equals(table)){
                return linedMapper.selectListTidByKey(key);
            }else if("shuizhi".equals(table)){
                return shuiZhiMapper.selectListTidBySearch(key);
            }

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return  null;

    }

    @Override
    public List<String> getListBaseTidByRequest(HttpServletRequest request, String table) {
        String search = request.getParameter("key");
        String selectdata = request.getParameter("selectdata");
        List<String> list = new ArrayList<>();
        if(selectdata != null && !selectdata.isEmpty()){
            String[] ids = selectdata.split(",");
            if(ids.length >0 ){
                list.addAll(Arrays.asList(ids));
            }
        }else {
            List<String> params = new ArrayList<>();
            if ("".equals(search) || search == null) {
                params.add("");
            } else if (search.contains(" ")) {
                params = Arrays.asList(search.split(" "));
            } else if (!search.contains(" ")) {
                params.add(search);
            }
            boolean firstSearch = true;
            for (String oneParam : params) {
                if (firstSearch) {
                    list = getListBaseTidBySearch(oneParam,table);
                    firstSearch = false;
                } else {
                    List<String> middleList = getListBaseTidBySearch(oneParam,table);
                    list.retainAll(middleList);
                }
            }
        }
        return list;
    }

    @Override
    public List findPagingByListTid(List<String> stringList, Integer pageIndex, Integer pageSize,String table) {
        try {
            if("village".equals(table)){
                return villageMapper.findPagingByListTid(stringList,pageIndex,pageSize);
            }else if("peasant".equals(table)){
                return peasantMapper.findPagingByListTid(stringList,pageIndex,pageSize);
            }else if("projectTable".equals(table)){
                return projectTableMapper.findPagingByListTid(stringList,pageIndex,pageSize);
            }else if("watercheck".equals(table)){
                return watercheckMapper.findPagingByListTid(stringList,pageIndex,pageSize);
            }else if("lined".equals(table)){
                return linedMapper.findPagingByListTid(stringList,pageIndex,pageSize);
            }else if("shuizhi".equals(table)){
                return shuiZhiMapper.findPagingByListTid(stringList, pageIndex, pageSize);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return null;
    }

    @Override
    public List<String> getBaseTidByName(String name, String table) {
        try {
            if("village".equals(table)){
                return  villageMapper.getTidByName(name);
            }else if("peasant".equals(table)){
                return peasantMapper.getTidByName(name);
            }else if("projectTable".equals(table)){
                return projectTableMapper.getTidByName(name);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return null;
    }

    private  List<String> getProIDs(String key){
        List<Xzzd> list =xzMapper.selectByTitle(key);
        List<Integer> zrcIds = new ArrayList<>();
        if(list.get(0).getPid() == -1){
         Xzzd xzzd = list.get(0);
         List<Xzzd> xzzdList = xzMapper.selectVyPid(xzzd.getId());
           if(xzzdList.size()>0){
               for (Xzzd xz:xzzdList) {
               List<Xzzd> zrcs = xzMapper.selectVyPid(xz.getId());
               if(zrcs.size()>0){
                   for (Xzzd xzdw: zrcs) {
                       zrcIds.add(xzdw.getId());
                   }
               }else {
                   zrcIds = null;
               }
               }
           }
           else {
               zrcIds =null;
           }
        }else {
          Xzzd  xzzd  = xzMapper.selectById(list.get(0).getPid());
          if(xzzd.getPid() == -1){
             List<Xzzd> xzzdList = xzMapper.selectVyPid(list.get(0).getId());
              for (Xzzd xz: xzzdList) {
                zrcIds.add(xz.getId());
              }
          }else {
            zrcIds.add(list.get(0).getId());
          }

        }
       List<String> proIds = new ArrayList<>();
       if(zrcIds == null){
           return  null;
       }else {
           for (Integer id: zrcIds ) {
               ProAndXz proAndXz = proAndXzMapper.selectByZrcId(id);
               if(proAndXz != null){
                   proIds.add(proAndXz.getProId());
               }
           }

           HashSet<String> set = new HashSet<String>(proIds.size());
           List<String> result = new ArrayList<String>(proIds.size());
           for (String str : proIds) {
               if (set.add(str)) {
                   result.add(str);
               }
           }
           proIds.clear();
           proIds.addAll(result);
       }
       return  proIds;
    }
}
